home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / othergnu / indent~1.zoo / indent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  48.8 KB  |  1,726 lines

  1.  
  2. /* Copyright (c) 1992, Free Software Foundation, Inc.  All rights reserved.
  3.  
  4.    Copyright (c) 1985 Sun Microsystems, Inc. Copyright (c) 1980 The Regents
  5.    of the University of California. Copyright (c) 1976 Board of Trustees of
  6.    the University of Illinois. All rights reserved.
  7.  
  8.    Redistribution and use in source and binary forms are permitted
  9.    provided that
  10.    the above copyright notice and this paragraph are duplicated in all such
  11.    forms and that any documentation, advertising materials, and other
  12.    materials related to such distribution and use acknowledge that the
  13.    software was developed by the University of California, Berkeley, the
  14.    University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of
  15.    either University or Sun Microsystems may not be used to endorse or
  16.    promote products derived from this software without specific prior written
  17.    permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  18.    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
  19.    OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
  20.  
  21. #include "sys.h"
  22. #include "indent.h"
  23. #include <ctype.h>
  24.  
  25. void
  26. usage ()
  27. {
  28.   fprintf (stderr, "usage: indent file [-o outfile ] [ options ]\n");
  29.   fprintf (stderr, "       indent file1 file2 ... fileN [ options ]\n");
  30.   exit (1);
  31. }
  32.  
  33.  
  34. /* Stuff that needs to be shared with the rest of indent.
  35.    Documented in indent.h.  */
  36. char *labbuf;
  37. char *s_lab;
  38. char *e_lab;
  39. char *l_lab;
  40. char *codebuf;
  41. char *s_code;
  42. char *e_code;
  43. char *l_code;
  44. char *combuf;
  45. char *s_com;
  46. char *e_com;
  47. char *l_com;
  48. struct buf save_com;
  49. char *bp_save;
  50. char *be_save;
  51. int code_lines;
  52. int line_no;
  53. struct fstate keywordf;
  54. struct fstate stringf;
  55. struct fstate boxcomf;
  56. struct fstate blkcomf;
  57. struct fstate scomf;
  58. struct fstate bodyf;
  59. int break_comma;
  60.  
  61. /* Insure that BUFSTRUC has at least REQ more chars left, if not extend it.
  62.       Note:  This may change bufstruc.ptr.  */
  63. #define need_chars(bufstruc, req) \
  64.   if ((bufstruc.end - bufstruc.ptr + (req)) >= bufstruc.size) \
  65. {\
  66.          int cur_chars = bufstruc.end - bufstruc.ptr;\
  67.          bufstruc.size *= 2;\
  68.          bufstruc.ptr = xrealloc(bufstruc.ptr,bufstruc.size);\
  69.          bufstruc.end = bufstruc.ptr + cur_chars;\
  70. }
  71.  
  72. int else_or_endif;
  73.  
  74. /* true iff last keyword was an else */
  75. int last_else;
  76.  
  77. /* True if we have just encountered the end of an if (...), etc. (i.e. the
  78.    ')' of the if (...) was the last token).  The variable is set to 2 in
  79.    the middle of the main token reading loop and is decremented at the
  80.    beginning of the loop, so it will reach zero when the second token after
  81.    the ')' is read.  */
  82. int last_token_ends_sp;
  83.  
  84. /* current indentation for declarations */
  85. int dec_ind;
  86.  
  87. /* structure indentation levels */
  88. int *di_stack;
  89.  
  90. /* Currently allocated size of di_stack.  */
  91. int di_stack_alloc;
  92.  
  93. /* used when buffering up comments to remember that
  94.    a newline was passed over */
  95. int flushed_nl;
  96. int force_nl;
  97.  
  98. /* set to true when we see a case, so we will know what to do
  99.    with the following colon */
  100. int scase;
  101.  
  102. /* when true, we are in the expressin of if(...), while(...), etc. */
  103. int sp_sw;
  104.  
  105. /* when this is positive, we have seen a ? without
  106.    the matching : in a <c>?<s>:<s> construct */
  107. int squest;
  108.  
  109. static void
  110. indent (this_file)
  111.      struct file_buffer *this_file;
  112. {
  113.   register int i;
  114.   enum codes hd_type;
  115.   register char *t_ptr;
  116.   enum codes type_code;
  117.   int dec_ind;            /* current indentation for declarations */
  118.   int flushed_nl;
  119.   int force_nl;
  120.   int scase;            /* true when we've just see a case */
  121.   int sp_sw;            /* when true, we are in the expressin of
  122.                    if(...), while(...), etc. */
  123.  
  124.   /* True if we have just encountered the end of an if (...), etc. (i.e. the
  125.      ')' of the if (...) was the last token).  The variable is set to 2 in
  126.      the middle of the main token reading loop and is decremented at the
  127.      beginning of the loop, so it will reach zero when the second token after
  128.      the ')' is read.  */
  129.   int last_token_ends_sp;
  130.  
  131.   int squest;            /* when this is positive, we have seen a ?
  132.                    without the matching : in a <c>?<s>:<s>
  133.                    construct */
  134.   int last_else;        /* true iff last keyword was an else */
  135.  
  136.  
  137.   in_prog = in_prog_pos = this_file->data;
  138.   in_prog_size = this_file->size;
  139.  
  140.   hd_type = code_eof;
  141.   dec_ind = 0;
  142.   last_token_ends_sp = false;
  143.   last_else = false;
  144.   sp_sw = force_nl = false;
  145.   scase = false;
  146.  
  147.   if (com_ind <= 1)
  148.     com_ind = 2;        /* dont put normal comments before column 2 */
  149.   if (troff)
  150.     {
  151.       if (bodyf.font[0] == 0)
  152.     parsefont (&bodyf, "R");
  153.       if (scomf.font[0] == 0)
  154.     parsefont (&scomf, "I");
  155.       if (blkcomf.font[0] == 0)
  156.     blkcomf = scomf, blkcomf.size += 2;
  157.       if (boxcomf.font[0] == 0)
  158.     boxcomf = blkcomf;
  159.       if (stringf.font[0] == 0)
  160.     parsefont (&stringf, "L");
  161.       if (keywordf.font[0] == 0)
  162.     parsefont (&keywordf, "B");
  163.       writefdef (&bodyf, 'B');
  164.       writefdef (&scomf, 'C');
  165.       writefdef (&blkcomf, 'L');
  166.       writefdef (&boxcomf, 'X');
  167.       writefdef (&stringf, 'S');
  168.       writefdef (&keywordf, 'K');
  169.     }
  170.   if (block_comment_max_col <= 0)
  171.     block_comment_max_col = max_col;
  172.   if (decl_com_ind <= 0)    /* if not specified by user, set this */
  173.     decl_com_ind =
  174.       ljust_decl ? (com_ind <= 10 ? 2 : com_ind - 8) : com_ind;
  175.   if (continuation_indent == 0)
  176.     continuation_indent = ind_size;
  177.   fill_buffer ();        /* get first batch of stuff into input buffer */
  178.  
  179.   parse (semicolon);
  180.   {
  181.     register char *p = buf_ptr;
  182.     register col = 1;
  183.  
  184.     while (1)
  185.       {
  186.     if (*p == ' ')
  187.       col++;
  188.     else if (*p == '\t')
  189.       col = ((col - 1) & ~7) + 9;
  190.     else
  191.       break;
  192.     p++;
  193.       }
  194.     if (col > ind_size)
  195.       parser_state_tos->ind_level = parser_state_tos->i_l_follow = col;
  196.   }
  197.   if (troff)
  198.     {
  199.       register char *p = in_name, *beg = in_name;
  200.  
  201.       while (*p)
  202.     if (*p++ == '/')
  203.       beg = p;
  204.       fprintf (output, ".Fn \"%s\"\n", beg);
  205.     }
  206.   /* START OF MAIN LOOP */
  207.  
  208.   while (1)
  209.     {                /* this is the main loop.  it will go until
  210.                    we reach eof */
  211.       int is_procname;
  212.  
  213.       type_code = lexi ();    /* lexi reads one token.  "token" points to
  214.                    the actual characters. lexi returns a code
  215.                    indicating the type of token */
  216.  
  217.       if (last_token_ends_sp > 0)
  218.     last_token_ends_sp--;
  219.       is_procname = parser_state_tos->procname[0];
  220.  
  221.       /* The following code moves everything following an if (), while (),
  222.          else, etc. up to the start of the following stmt to a buffer. This
  223.          allows proper handling of both kinds of brace placement. */
  224.  
  225.       flushed_nl = false;
  226.       while (parser_state_tos->search_brace)
  227.     {
  228.       /* After scanning an if(), while (), etc., it might be necessary to
  229.          keep track of the text between the if() and the start of the
  230.          statement which follows.  Use save_com to do so.  */
  231.  
  232.       switch (type_code)
  233.         {
  234.         case newline:
  235.           ++line_no;
  236.           flushed_nl = true;
  237.         case form_feed:
  238.           break;        /* form feeds and newlines found here will be
  239.                    ignored */
  240.  
  241.         case lbrace:    /* this is a brace that starts the compound
  242.                    stmt */
  243.           if (save_com.end == save_com.ptr)
  244.         {
  245.           /* ignore buffering if a comment wasnt stored up */
  246.           parser_state_tos->search_brace = false;
  247.           goto check_type;
  248.         }
  249.           /* We need to put the '{' back into save_com somewhere.  */
  250.           if (btype_2)
  251.         /* Put it at the beginning, e.g. if (foo) { / * comment here *
  252.            / */
  253.  
  254.         save_com.ptr[0] = '{';
  255.  
  256.           else
  257.         {
  258.           /* Put it at the end, e.g. if (foo) / * comment here * / { */
  259.  
  260.           /* Putting in this newline causes a dump_line to occur
  261.              right after the comment, thus insuring that it will be
  262.              put in the correct column.  */
  263.           *save_com.end++ = '\n';
  264.           *save_com.end++ = '{';
  265.         }
  266.  
  267.           /* Go to common code to get out of this loop.  */
  268.           goto sw_buffer;
  269.  
  270.         case comment:    /* we have a comment, so we must copy it into
  271.                    the buffer */
  272.           if (!flushed_nl || save_com.end != save_com.ptr)
  273.         {
  274.           need_chars (save_com, 10);
  275.           if (save_com.end == save_com.ptr)
  276.             {        /* if this is the first comment, we must set
  277.                    up the buffer */
  278.               save_com.ptr[0] = save_com.ptr[1] = ' ';
  279.               save_com.end = save_com.ptr + 2;
  280.             }
  281.           else
  282.             {
  283.               *save_com.end++ = '\n';    /* add newline between
  284.                            comments */
  285.               *save_com.end++ = ' ';
  286.               --line_no;
  287.             }
  288.           *save_com.end++ = '/';    /* copy in start of comment */
  289.           *save_com.end++ = '*';
  290.  
  291.           for (;;)
  292.             {        /* loop until we get to the end of the
  293.                    comment */
  294.               /* make sure there is room for this character and
  295.                  (while we're at it) the '/' we might add at the end
  296.                  of the loop. */
  297.               need_chars (save_com, 2);
  298.               *save_com.end = *buf_ptr++;
  299.               if (buf_ptr >= buf_end)
  300.             {
  301.               fill_buffer ();
  302.               if (had_eof)
  303.                 {
  304.                   diag (1, "Unclosed comment");
  305.                   exit (1);
  306.                 }
  307.             }
  308.  
  309.               if (*save_com.end++ == '*' && *buf_ptr == '/')
  310.             break;    /* we are at end of comment */
  311.  
  312.             }
  313.           *save_com.end++ = '/';    /* add ending slash */
  314.           if (++buf_ptr >= buf_end)    /* get past / in buffer */
  315.             fill_buffer ();
  316.           break;
  317.         }
  318.         default:        /* it is the start of a normal statment */
  319.           if (flushed_nl)    /* if we flushed a newline, make sure it is
  320.                    put back */
  321.         force_nl = true;
  322.           if ((type_code == sp_paren && *token == 'i'
  323.            && last_else && else_if)
  324.           ||
  325.           (type_code == sp_nparen && *token == 'e'
  326.            && e_code != s_code && e_code[-1] == '}'))
  327.         force_nl = false;
  328.  
  329.           if (save_com.end == save_com.ptr)
  330.         {
  331.           /* ignore buffering if comment wasnt saved up */
  332.           parser_state_tos->search_brace = false;
  333.           goto check_type;
  334.         }
  335.           if (force_nl)
  336.         {        /* if we should insert a nl here, put it into
  337.                    the buffer */
  338.           force_nl = false;
  339.           --line_no;    /* this will be re-increased when the nl is
  340.                    read from the buffer */
  341.           need_chars (save_com, 2);
  342.           *save_com.end++ = '\n';
  343.           *save_com.end++ = ' ';
  344.           if (verbose && !flushed_nl)    /* print error msg if the
  345.                            line was not already
  346.                            broken */
  347.             diag (0, "Line broken");
  348.           flushed_nl = false;
  349.         }
  350.           for (t_ptr = token; t_ptr < token_end; ++t_ptr)
  351.         {
  352.           need_chars (save_com, 1);
  353.           *save_com.end++ = *t_ptr;    /* copy token into temp
  354.                            buffer */
  355.         }
  356.           parser_state_tos->procname = "\0";
  357.  
  358.         sw_buffer:
  359.           parser_state_tos->search_brace = false;    /* stop looking for
  360.                                start of stmt */
  361.           bp_save = buf_ptr;/* save current input buffer */
  362.           be_save = buf_end;
  363.           buf_ptr = save_com.ptr;    /* fix so that subsequent calls to
  364.                        lexi will take tokens out of
  365.                        save_com */
  366.           need_chars (save_com, 1);
  367.           *save_com.end++ = ' ';    /* add trailing blank, just in case */
  368.           buf_end = save_com.end;
  369.           save_com.end = save_com.ptr;    /* make save_com empty */
  370.           break;
  371.         }            /* end of switch */
  372.       /* we must make this check, just in case there was an unexpected
  373.          EOF */
  374.       if (type_code != code_eof)
  375.         type_code = lexi ();/* read another token */
  376.       /* if (parser_state_tos->search_brace)
  377.          parser_state_tos->procname[0] = 0; */
  378.       if ((is_procname = parser_state_tos->procname[0]) && flushed_nl
  379.           && !procnames_start_line && parser_state_tos->in_decl
  380.           && type_code == ident)
  381.         flushed_nl = 0;
  382.     }            /* end of while (search_brace) */
  383.       last_else = 0;
  384.  
  385.     check_type:
  386.       if (type_code == code_eof)
  387.     {            /* we got eof */
  388.       if (s_lab != e_lab || s_code != e_code
  389.           || s_com != e_com)/* must dump end of line */
  390.         dump_line ();
  391.       if (parser_state_tos->tos > 1)    /* check for balanced braces */
  392.         diag (1, "Stuff missing from end of file.");
  393.  
  394.       if (verbose)
  395.         {
  396.           printf ("There were %d output lines and %d comments\n",
  397.               out_lines, out_coms);
  398.           printf ("(Lines with comments)/(Lines with code): %6.3f\n",
  399.               (1.0 * com_lines) / code_lines);
  400.         }
  401.       fflush (output);
  402.       if (found_err)
  403.         exit (found_err);
  404.  
  405.       return;
  406.     }
  407.  
  408.       if ((type_code != comment) &&
  409.       (type_code != newline) &&
  410.       (type_code != preesc) &&
  411.       (type_code != form_feed))
  412.     {
  413.       if (force_nl &&
  414.           (type_code != semicolon) &&
  415.           (type_code != lbrace || !btype_2))
  416.         {
  417.           /* we should force a broken line here */
  418.           if (verbose && !flushed_nl)
  419.         diag (0, "Line broken");
  420.           flushed_nl = false;
  421.           dump_line ();
  422.           parser_state_tos->want_blank = false;    /* dont insert blank at
  423.                                line start */
  424.           force_nl = false;
  425.         }
  426.       parser_state_tos->in_stmt = true;    /* turn on flag which causes
  427.                            an extra level of
  428.                            indentation. this is
  429.                            turned off by a ; or } */
  430.       if (s_com != e_com)
  431.         {            /* the turkey has embedded a comment in a
  432.                    line. Move it from the com buffer to the
  433.                    code buffer.  */
  434.           /* Do not add a space before the comment if it is the first
  435.              thing on the line.  */
  436.           if (e_code != s_code)
  437.         {
  438.           *e_code++ = ' ';
  439.         }
  440.           for (t_ptr = s_com; *t_ptr; ++t_ptr)
  441.         {
  442.           check_code_size;
  443.           *e_code++ = *t_ptr;
  444.         }
  445.           *e_code++ = ' ';
  446.           *e_code = '\0';    /* null terminate code sect */
  447.           parser_state_tos->want_blank = false;
  448.           e_com = s_com;
  449.         }
  450.     }
  451.       else if (type_code != comment)    /* preserve force_nl thru a comment */
  452.     force_nl = false;    /* cancel forced newline after newline, form
  453.                    feed, etc */
  454.  
  455.  
  456.  
  457.       /*-----------------------------------------------------*\
  458.       |       do switch on type of token scanned        |
  459.       \*-----------------------------------------------------*/
  460.       check_code_size;
  461.       switch (type_code)
  462.     {            /* now, decide what to do with the token */
  463.  
  464.     case form_feed:    /* found a form feed in line */
  465.       parser_state_tos->use_ff = true;    /* a form feed is treated
  466.                            much like a newline */
  467.       dump_line ();
  468.       parser_state_tos->want_blank = false;
  469.       break;
  470.  
  471.     case newline:
  472.       if (parser_state_tos->last_token != comma
  473.           || parser_state_tos->p_l_follow > 0
  474.           || !leave_comma || parser_state_tos->block_init
  475.           || !break_comma || s_com != e_com)
  476.         {
  477.           dump_line ();
  478.           parser_state_tos->want_blank = false;
  479.         }
  480.       /* If we were on the line with a #else or a #endif, we aren't
  481.          anymore.  */
  482.       else_or_endif = false;
  483.       ++line_no;        /* keep track of input line number */
  484.       break;
  485.  
  486.     case lparen:
  487.       /* Braces in initializer lists should be put on new lines. This is
  488.          necessary so that -gnu does not cause things like char
  489.          *this_is_a_string_array[] = { "foo", "this_string_does_not_fit",
  490.          "nor_does_this_rather_long_string" } which is what happens
  491.          because we are trying to line the strings up with the
  492.          parentheses, and those that are too long are moved to the right
  493.          an ugly amount.
  494.     
  495.          However, if the current line is empty, the left brace is
  496.          already on a new line, so don't molest it.  */
  497.       if (token[0] == '{'
  498.           && (s_code != e_code || s_com != e_com || s_lab != e_lab))
  499.         {
  500.           dump_line ();
  501.           /* Do not put a space before the '{'.  */
  502.           parser_state_tos->want_blank = false;
  503.         }
  504.  
  505.       /* Count parens so we know how deep we are.  */
  506.       if (++parser_state_tos->p_l_follow
  507.           >= parser_state_tos->paren_indents_size)
  508.         {
  509.           parser_state_tos->paren_indents_size *= 2;
  510.           parser_state_tos->paren_indents = (short *)
  511.         xrealloc (parser_state_tos->paren_indents,
  512.               parser_state_tos->paren_indents_size
  513.               * sizeof (short));
  514.         }
  515.       parser_state_tos->paren_depth++;
  516.       if (parser_state_tos->want_blank && *token != '['
  517.           && (parser_state_tos->last_token != ident || proc_calls_space
  518.           || (parser_state_tos->its_a_keyword
  519.               && (!parser_state_tos->sizeof_keyword
  520.               || blank_after_sizeof))))
  521.         *e_code++ = ' ';
  522.  
  523.       if (parser_state_tos->in_decl && !parser_state_tos->block_init)
  524.         if (troff
  525.         && !parser_state_tos->dumped_decl_indent
  526.         && !is_procname
  527.         && parser_state_tos->last_token == decl)
  528.           {
  529.         parser_state_tos->dumped_decl_indent = 1;
  530.         sprintf (e_code, "\n.Du %dp+\200p \"%.*s\"\n", dec_ind * 7,
  531.              token_end - token, token);
  532.         e_code += strlen (e_code);
  533.           }
  534.         else
  535.           {
  536.         while ((e_code - s_code) < dec_ind)
  537.           {
  538.             check_code_size;
  539.             *e_code++ = ' ';
  540.           }
  541.         *e_code++ = token[0];
  542.           }
  543.       else
  544.         *e_code++ = token[0];
  545.  
  546.       parser_state_tos->paren_indents[parser_state_tos->p_l_follow - 1]
  547.         = e_code - s_code;
  548.       if (sp_sw && parser_state_tos->p_l_follow == 1
  549.           && extra_expression_indent
  550.           && parser_state_tos->paren_indents[0] < 2 * ind_size)
  551.         parser_state_tos->paren_indents[0] = 2 * ind_size;
  552.       parser_state_tos->want_blank = false;
  553.  
  554.       if (parser_state_tos->in_or_st
  555.           && *token == '('
  556.           && parser_state_tos->tos <= 2)
  557.         {
  558.           /* this is a kluge to make sure that declarations will be
  559.              aligned right if proc decl has an explicit type on it, i.e.
  560.              "int a(x) {..." */
  561.           parse_lparen_in_decl ();
  562.  
  563.           /* Turn off flag for structure decl or initialization.  */
  564.           parser_state_tos->in_or_st = false;
  565.         }
  566.       if (parser_state_tos->sizeof_keyword)
  567.         parser_state_tos->sizeof_mask |= 1 << parser_state_tos->p_l_follow;
  568.  
  569.       /* The '(' that starts a cast can never be preceeded by an
  570.          indentifier or decl.  */
  571.       if (parser_state_tos->last_token == decl
  572.           || (parser_state_tos->last_token == ident
  573.           && parser_state_tos->last_rw != rw_return))
  574.         parser_state_tos->noncast_mask |=
  575.           1 << parser_state_tos->p_l_follow;
  576.       else
  577.         parser_state_tos->noncast_mask &=
  578.           ~(1 << parser_state_tos->p_l_follow);
  579.  
  580.       break;
  581.  
  582.     case rparen:
  583.       parser_state_tos->paren_depth--;
  584.       if (parser_state_tos->cast_mask
  585.           & (1 << parser_state_tos->p_l_follow)
  586.           & ~parser_state_tos->sizeof_mask)
  587.         {
  588.           parser_state_tos->last_u_d = true;
  589.           parser_state_tos->cast_mask &=
  590.         (1 << parser_state_tos->p_l_follow) - 1;
  591.           if (!parser_state_tos->cast_mask && cast_space)
  592.         parser_state_tos->want_blank = true;
  593.           else
  594.         parser_state_tos->want_blank = false;
  595.         }
  596.       else if (parser_state_tos->in_decl
  597.            && ! parser_state_tos->block_init
  598.            && parser_state_tos->paren_depth == 0)
  599.         parser_state_tos->want_blank = true;
  600.  
  601.       parser_state_tos->sizeof_mask
  602.         &= (1 << parser_state_tos->p_l_follow) - 1;
  603.       if (--parser_state_tos->p_l_follow < 0)
  604.         {
  605.           parser_state_tos->p_l_follow = 0;
  606.           diag (0, "Extra %c", *token);
  607.         }
  608.  
  609.       /* if the paren starts the line, then indent it */
  610.       if (e_code == s_code)
  611.         {
  612.           int level = parser_state_tos->p_l_follow;
  613.           parser_state_tos->paren_level = level;
  614.           if (level > 0)
  615.         paren_target = -parser_state_tos->paren_indents[level - 1];
  616.           else
  617.         paren_target = 0;
  618.         }
  619.       *e_code++ = token[0];
  620.  
  621. #if 0
  622.       if (!parser_state_tos->cast_mask || cast_space)
  623.         parser_state_tos->want_blank = true;
  624. #endif
  625.  
  626.       /* check for end of if (...), or some such */
  627.       if (sp_sw && (parser_state_tos->p_l_follow == 0))
  628.         {
  629.  
  630.           /* Indicate that we have just left the parenthesized expression
  631.              of a while, if, or for, unless we are getting out of the
  632.              parenthesized expression of the while of a do-while loop.
  633.              (do-while is different because a semicolon immediately
  634.              following this will not indicate a null loop body).  */
  635.           if (parser_state_tos->p_stack[parser_state_tos->tos]
  636.           != dohead)
  637.         last_token_ends_sp = 2;
  638.           sp_sw = false;
  639.           force_nl = true;    /* must force newline after if */
  640.           parser_state_tos->last_u_d = true;    /* inform lexi that a
  641.                                following operator is
  642.                                unary */
  643.           parser_state_tos->in_stmt = false;    /* dont use stmt
  644.                                continuation
  645.                                indentation */
  646.  
  647.           parse (hd_type);    /* let parser worry about if, or whatever */
  648.         }
  649.       parser_state_tos->search_brace = btype_2;    /* this should insure
  650.                                that constructs such
  651.                                as main(){...} and
  652.                                int[]{...} have their
  653.                                braces put in the
  654.                                right place */
  655.       break;
  656.  
  657.     case unary_op:        /* this could be any unary operation */
  658.       if (parser_state_tos->want_blank)
  659.         *e_code++ = ' ';
  660.  
  661.       if (troff
  662.           && !parser_state_tos->dumped_decl_indent
  663.           && parser_state_tos->in_decl && !is_procname)
  664.         {
  665.           sprintf (e_code, "\n.Du %dp+\200p \"%.*s\"\n", dec_ind * 7,
  666.                token_end - token, token);
  667.           parser_state_tos->dumped_decl_indent = 1;
  668.           e_code += strlen (e_code);
  669.         }
  670.       else
  671.         {
  672.           char *res = token;
  673.           char *res_end = token_end;
  674.  
  675.           /* if this is a unary op in a declaration, we should
  676.          indent this token */
  677.           if (parser_state_tos->paren_depth == 0
  678.           && parser_state_tos->in_decl
  679.           && !parser_state_tos->block_init)
  680.         {
  681.           while ((e_code - s_code) < (dec_ind - (token_end - token)))
  682.             {
  683.               check_code_size;
  684.               *e_code++ = ' ';
  685.             }
  686.         }
  687.  
  688.           if (troff && token[0] == '-' && token[1] == '>')
  689.         {
  690.           static char resval[] = "\\(->";
  691.           res = resval;
  692.           res_end = res + sizeof (resval);
  693.         }
  694.  
  695.           for (t_ptr = res; t_ptr < res_end; ++t_ptr)
  696.         {
  697.           check_code_size;
  698.           *e_code++ = *t_ptr;
  699.         }
  700.         }
  701.       parser_state_tos->want_blank = false;
  702.       break;
  703.  
  704.     case binary_op:    /* any binary operation */
  705.       if (parser_state_tos->want_blank
  706.           || (e_code > s_code && *e_code != ' '))
  707.         *e_code++ = ' ';
  708.  
  709.       {
  710.         char *res = token;
  711.         char *res_end = token_end;
  712. #define set_res(str) \
  713.           {\
  714.         static char resval[] = str;\
  715.         res = resval;\
  716.         res_end = res + sizeof(resval);\
  717.           }
  718.  
  719.         if (troff)
  720.           switch (token[0])
  721.         {
  722.         case '<':
  723.           if (token[1] == '=')
  724.             set_res ("\\(<=");
  725.           break;
  726.         case '>':
  727.           if (token[1] == '=')
  728.             set_res ("\\(>=");
  729.           break;
  730.         case '!':
  731.           if (token[1] == '=')
  732.             set_res ("\\(!=");
  733.           break;
  734.         case '|':
  735.           if (token[1] == '|')
  736.             {
  737.               set_res ("\\(br\\(br");
  738.             }
  739.           else if (token[1] == 0)
  740.             set_res ("\\(br");
  741.           break;
  742.         }
  743.  
  744.         for (t_ptr = res; t_ptr < res_end; ++t_ptr)
  745.           {
  746.         check_code_size;
  747.         *e_code++ = *t_ptr;    /* move the operator */
  748.           }
  749.       }
  750.       parser_state_tos->want_blank = true;
  751.       break;
  752.  
  753.     case postop:        /* got a trailing ++ or -- */
  754.       *e_code++ = token[0];
  755.       *e_code++ = token[1];
  756.       parser_state_tos->want_blank = true;
  757.       break;
  758.  
  759.     case question:        /* got a ? */
  760.       squest++;        /* this will be used when a later colon
  761.                    appears so we can distinguish the
  762.                    <c>?<n>:<n> construct */
  763.       if (parser_state_tos->want_blank)
  764.         *e_code++ = ' ';
  765.       *e_code++ = '?';
  766.       parser_state_tos->want_blank = true;
  767.       break;
  768.  
  769.     case casestmt:        /* got word 'case' or 'default' */
  770.       scase = true;        /* so we can process the later colon properly */
  771.       goto copy_id;
  772.  
  773.     case colon:        /* got a ':' */
  774.       if (squest > 0)
  775.         {            /* it is part of the <c>?<n>: <n> construct */
  776.           --squest;
  777.           if (parser_state_tos->want_blank)
  778.         *e_code++ = ' ';
  779.           *e_code++ = ':';
  780.           parser_state_tos->want_blank = true;
  781.           break;
  782.         }
  783.       if (parser_state_tos->in_decl)
  784.         {
  785.           *e_code++ = ':';
  786.           parser_state_tos->want_blank = false;
  787.           break;
  788.         }
  789.       parser_state_tos->in_stmt = false;    /* seeing a label does not
  790.                            imply we are in a stmt */
  791.       for (t_ptr = s_code; *t_ptr; ++t_ptr)
  792.         *e_lab++ = *t_ptr;    /* turn everything so far into a label */
  793.       e_code = s_code;
  794.       *e_lab++ = ':';
  795.       *e_lab++ = ' ';
  796.       *e_lab = '\0';
  797.       /* parser_state_tos->pcas e will be used by dump_line to decide
  798.          how to indent the label. force_nl will force a case n: to be
  799.          on a line by itself */
  800.       force_nl = parser_state_tos->pcase = scase;
  801.       scase = false;
  802.       parser_state_tos->want_blank = false;
  803.       break;
  804.  
  805.     case semicolon:
  806.       /* we are not in an initialization or structure declaration */
  807.       parser_state_tos->in_or_st = false;
  808.       scase = false;
  809.       squest = 0;
  810.       /* The following code doesn't seem to do much good. Just because
  811.          we've found something like extern int foo();    or int (*foo)();
  812.          doesn't mean we are out of a declaration.  Now if it was serving
  813.          some purpose we'll have to address that.... if
  814.          (parser_state_tos->last_token == rparen)
  815.          parser_state_tos->in_parameter_declaration = 0; */
  816.       parser_state_tos->cast_mask = 0;
  817.       parser_state_tos->sizeof_mask = 0;
  818.       parser_state_tos->block_init = 0;
  819.       parser_state_tos->block_init_level = 0;
  820.       parser_state_tos->just_saw_decl--;
  821.  
  822.       if (parser_state_tos->in_decl
  823.           && s_code == e_code
  824.           && !parser_state_tos->block_init)
  825.         while ((e_code - s_code) < (dec_ind - 1))
  826.           {
  827.         check_code_size;
  828.         *e_code++ = ' ';
  829.           }
  830.  
  831.       /* if we were in a first level structure declaration,
  832.          we aren't any more */
  833.       parser_state_tos->in_decl = (parser_state_tos->dec_nest > 0);
  834.       if ((!sp_sw || hd_type != forstmt)
  835.           && parser_state_tos->p_l_follow > 0)
  836.         {
  837.  
  838.           /* This should be true iff there were unbalanced parens in the
  839.              stmt.  It is a bit complicated, because the semicolon might
  840.              be in a for stmt */
  841.           diag (1, "Unbalanced parens");
  842.           parser_state_tos->p_l_follow = 0;
  843.           if (sp_sw)
  844.         {        /* this is a check for a if, while, etc. with
  845.                    unbalanced parens */
  846.           sp_sw = false;
  847.           parse (hd_type);    /* dont lose the if, or whatever */
  848.         }
  849.         }
  850.  
  851.       /* If we have a semicolon following an if, while, or for, and the
  852.          user wants us to, we should insert a space (to show that there
  853.          is a null statement there).  */
  854.       if (last_token_ends_sp && space_sp_semicolon)
  855.         {
  856.           *e_code++ = ' ';
  857.         }
  858.       *e_code++ = ';';
  859.       parser_state_tos->want_blank = true;
  860.       /* we are no longer in the middle of a stmt */
  861.       parser_state_tos->in_stmt = (parser_state_tos->p_l_follow > 0);
  862.  
  863.       if (!sp_sw)
  864.         {            /* if not if for (;;) */
  865.           parse (semicolon);/* let parser know about end of stmt */
  866.           force_nl = true;    /* force newline after a end of stmt */
  867.         }
  868.       break;
  869.  
  870.     case lbrace:        /* got a '{' */
  871.       parser_state_tos->in_stmt = false;    /* dont indent the {} */
  872.       if (!parser_state_tos->block_init)
  873.         force_nl = true;    /* force other stuff on same line as '{' onto
  874.                    new line */
  875.       else if (parser_state_tos->block_init_level <= 0)
  876.         parser_state_tos->block_init_level = 1;
  877.       else
  878.         parser_state_tos->block_init_level++;
  879.  
  880.       if (s_code != e_code && !parser_state_tos->block_init)
  881.         {
  882.           if (!btype_2)
  883.         {
  884.           dump_line ();
  885.           parser_state_tos->want_blank = false;
  886.         }
  887.           else
  888.         {
  889.           if (parser_state_tos->in_parameter_declaration
  890.               && !parser_state_tos->in_or_st)
  891.             {
  892.               parser_state_tos->i_l_follow = 0;
  893.               dump_line ();
  894.               parser_state_tos->want_blank = false;
  895.             }
  896.           else
  897.             parser_state_tos->want_blank = true;
  898.         }
  899.         }
  900.       if (parser_state_tos->in_parameter_declaration)
  901.         prefix_blankline_requested = 0;
  902.  
  903.       if (parser_state_tos->p_l_follow > 0)
  904.         {            /* check for preceeding unbalanced parens */
  905.           diag (1, "Unbalanced parens");
  906.           parser_state_tos->p_l_follow = 0;
  907.           if (sp_sw)
  908.         {        /* check for unclosed if, for, etc. */
  909.           sp_sw = false;
  910.           parse (hd_type);
  911.           parser_state_tos->ind_level = parser_state_tos->i_l_follow;
  912.         }
  913.         }
  914.       if (s_code == e_code)
  915.         parser_state_tos->ind_stmt = false;    /* dont put extra indentation
  916.                            on line with '{' */
  917.       if (parser_state_tos->in_decl && parser_state_tos->in_or_st)
  918.         {
  919.           /* This is a structure declaration.  */
  920.           if (parser_state_tos->dec_nest >= di_stack_alloc)
  921.         {
  922.           di_stack_alloc *= 2;
  923.           di_stack = (int *)
  924.             xrealloc (di_stack,
  925.                   di_stack_alloc * sizeof (*di_stack));
  926.         }
  927.           di_stack[parser_state_tos->dec_nest++] = dec_ind;
  928.           /* ?        dec_ind = 0; */
  929.         }
  930.       else
  931.         {
  932.           parser_state_tos->decl_on_line = false;    /* we cant be in the
  933.                                middle of a
  934.                                declaration, so dont
  935.                                do special
  936.                                indentation of
  937.                                comments */
  938.  
  939. #if 0                /* Doesn't work currently. */
  940.           if (blanklines_after_declarations_at_proctop
  941.           && parser_state_tos->in_parameter_declaration)
  942.         postfix_blankline_requested = 1;
  943. #endif
  944.           parser_state_tos->in_parameter_declaration = 0;
  945.         }
  946.       dec_ind = 0;
  947.  
  948.       /* We are no longer looking for an initializer or structure. Needed
  949.          so that the '=' in "enum bar {a = 1" does not get interpreted as
  950.          the start of an initializer.  */
  951.       parser_state_tos->in_or_st = false;
  952.  
  953.       parse (lbrace);    /* let parser know about this */
  954.       if (parser_state_tos->want_blank)    /* put a blank before '{' if
  955.                            '{' is not at start of
  956.                            line */
  957.         *e_code++ = ' ';
  958.       parser_state_tos->want_blank = false;
  959.       *e_code++ = '{';
  960.       parser_state_tos->just_saw_decl = 0;
  961.       break;
  962.  
  963.     case rbrace:        /* got a '}' */
  964.       /* semicolons can be omitted in declarations */
  965.       if (parser_state_tos->p_stack[parser_state_tos->tos] == decl
  966.           && !parser_state_tos->block_init)
  967.         parse (semicolon);
  968.       if (parser_state_tos->p_l_follow)
  969.         {            /* check for unclosed if, for, else. */
  970.           diag (1, "Unbalanced parens");
  971.           parser_state_tos->p_l_follow = 0;
  972.           sp_sw = false;
  973.         }
  974.       parser_state_tos->just_saw_decl = 0;
  975.       parser_state_tos->block_init_level--;
  976.       if (s_code != e_code && !parser_state_tos->block_init)
  977.         {            /* '}' must be first on line */
  978.           if (verbose)
  979.         diag (0, "Line broken");
  980.           dump_line ();
  981.         }
  982.       *e_code++ = '}';
  983.       parser_state_tos->want_blank = true;
  984.       parser_state_tos->in_stmt = parser_state_tos->ind_stmt = false;
  985.       if (parser_state_tos->dec_nest > 0)
  986.         {            /* we are in multi-level structure
  987.                    declaration */
  988.           dec_ind = di_stack[--parser_state_tos->dec_nest];
  989.           if (parser_state_tos->dec_nest == 0
  990.           && !parser_state_tos->in_parameter_declaration)
  991.         parser_state_tos->just_saw_decl = 2;
  992.           parser_state_tos->in_decl = true;
  993.         }
  994.       prefix_blankline_requested = 0;
  995.       parse (rbrace);    /* let parser know about this */
  996.       if (parser_state_tos->p_stack[parser_state_tos->tos] == dohead
  997.           && !btype_2)
  998.         force_nl = true;
  999.       parser_state_tos->search_brace
  1000.         = (cuddle_else
  1001.          && parser_state_tos->p_stack[parser_state_tos->tos] == ifhead);
  1002.  
  1003. #if 0
  1004.       parser_state_tos->search_brace
  1005.         = (cuddle_else
  1006.            && parser_state_tos->p_stack[parser_state_tos->tos] == ifhead
  1007.            && (parser_state_tos->il[parser_state_tos->tos] >= parser_state_tos->ind_level));
  1008. #endif
  1009.  
  1010.       if (parser_state_tos->tos <= 1
  1011.           && blanklines_after_procs
  1012.           && parser_state_tos->dec_nest <= 0)
  1013.         postfix_blankline_requested = 1;
  1014.       break;
  1015.  
  1016.     case swstmt:        /* got keyword "switch" */
  1017.       sp_sw = true;
  1018.       hd_type = swstmt;    /* keep this for when we have seen the
  1019.                    expression */
  1020.       goto copy_id;        /* go move the token into buffer */
  1021.  
  1022.     case sp_paren:        /* token is if, while, for */
  1023.       sp_sw = true;        /* the interesting stuff is done after the
  1024.                    expression is scanned */
  1025.       hd_type = (*token == 'i' ? ifstmt :
  1026.              (*token == 'w' ? whilestmt : forstmt));
  1027.  
  1028.       /* remember the type of header for later use by parser */
  1029.       goto copy_id;        /* copy the token into line */
  1030.  
  1031.     case sp_nparen:    /* got else, do */
  1032.       parser_state_tos->in_stmt = false;
  1033.       if (*token == 'e')
  1034.         {
  1035.           if (e_code != s_code && (!cuddle_else || e_code[-1] != '}'))
  1036.         {
  1037.           if (verbose)
  1038.             diag (0, "Line broken");
  1039.           dump_line ();    /* make sure this starts a line */
  1040.           parser_state_tos->want_blank = false;
  1041.         }
  1042.           force_nl = true;    /* also, following stuff must go onto new
  1043.                    line */
  1044.           last_else = 1;
  1045.           parse (elselit);
  1046.         }
  1047.       else
  1048.         {
  1049.           if (e_code != s_code)
  1050.         {        /* make sure this starts a line */
  1051.           if (verbose)
  1052.             diag (0, "Line broken");
  1053.           dump_line ();
  1054.           parser_state_tos->want_blank = false;
  1055.         }
  1056.           force_nl = true;    /* also, following stuff must go onto new
  1057.                    line */
  1058.           last_else = 0;
  1059.           parse (dolit);
  1060.         }
  1061.       goto copy_id;        /* move the token into line */
  1062.  
  1063.     case decl:        /* we have a declaration type (int, register,
  1064.                    etc.) */
  1065.  
  1066.       parse (decl);        /* let parser worry about indentation */
  1067.       if (parser_state_tos->last_token == rparen
  1068.           && parser_state_tos->tos <= 1)
  1069.         {
  1070.           parser_state_tos->in_parameter_declaration = 1;
  1071.           if (s_code != e_code)
  1072.         {
  1073.           dump_line ();
  1074.           parser_state_tos->want_blank = false;
  1075.         }
  1076.         }
  1077.       if (parser_state_tos->in_parameter_declaration
  1078.           && indent_parameters
  1079.           && parser_state_tos->dec_nest == 0
  1080.           && parser_state_tos->p_l_follow == 0)
  1081.         {
  1082.           parser_state_tos->ind_level = parser_state_tos->i_l_follow = indent_parameters;
  1083.           parser_state_tos->ind_stmt = 0;
  1084.         }
  1085.  
  1086.       /* in_or_st set for struct or initialization decl. Don't set it if
  1087.          we're in ansi prototype */
  1088.       if (!parser_state_tos->paren_depth)
  1089.         parser_state_tos->in_or_st = true;
  1090.  
  1091.       parser_state_tos->in_decl = parser_state_tos->decl_on_line = true;
  1092. #if 0
  1093.       if (!parser_state_tos->in_or_st && parser_state_tos->dec_nest <= 0)
  1094. #endif
  1095.         if (parser_state_tos->dec_nest <= 0)
  1096.           parser_state_tos->just_saw_decl = 2;
  1097.       if (prefix_blankline_requested
  1098.           && (parser_state_tos->block_init != 0
  1099.           || parser_state_tos->block_init_level != -1
  1100.           || parser_state_tos->last_token != rbrace
  1101.           || e_code != s_code
  1102.           || e_lab != s_lab
  1103.           || e_com != s_com))
  1104.         prefix_blankline_requested = 0;
  1105.       i = token_end - token + 1;    /* get length of token plus 1 */
  1106.  
  1107.       /* dec_ind = e_code - s_code + (parser_state_tos->decl_indent>i ?
  1108.          parser_state_tos->decl_indent : i); */
  1109.       dec_ind = decl_indent > 0 ? decl_indent : i;
  1110.       goto copy_id;
  1111.  
  1112.     case ident:        /* got an identifier or constant */
  1113.       /* If we are in a declaration, we must indent identifier. But not
  1114.          inside the parentheses of an ANSI function declaration.  */
  1115.       if (parser_state_tos->in_decl
  1116.           && parser_state_tos->p_l_follow == 0
  1117.           && parser_state_tos->last_token != rbrace)
  1118.         {
  1119.           if (parser_state_tos->want_blank)
  1120.         *e_code++ = ' ';
  1121.           parser_state_tos->want_blank = false;
  1122.           if (is_procname == 0 || !procnames_start_line)
  1123.         {
  1124.           if (!parser_state_tos->block_init)
  1125.             if (troff && !parser_state_tos->dumped_decl_indent)
  1126.               {
  1127.             sprintf (e_code, "\n.De %dp+\200p\n", dec_ind * 7);
  1128.             parser_state_tos->dumped_decl_indent = 1;
  1129.             e_code += strlen (e_code);
  1130.               }
  1131.             else
  1132.               while ((e_code - s_code) < dec_ind)
  1133.             {
  1134.               check_code_size;
  1135.               *e_code++ = ' ';
  1136.             }
  1137.         }
  1138.           else
  1139.         {
  1140.           if (dec_ind && s_code != e_code)
  1141.             dump_line ();
  1142.           dec_ind = 0;
  1143.           parser_state_tos->want_blank = false;
  1144.         }
  1145.         }
  1146.       else if (sp_sw && parser_state_tos->p_l_follow == 0)
  1147.         {
  1148.           sp_sw = false;
  1149.           force_nl = true;
  1150.           parser_state_tos->last_u_d = true;
  1151.           parser_state_tos->in_stmt = false;
  1152.           parse (hd_type);
  1153.         }
  1154.     copy_id:
  1155.       if (parser_state_tos->want_blank)
  1156.         *e_code++ = ' ';
  1157.       if (troff && parser_state_tos->its_a_keyword)
  1158.         {
  1159.           e_code = chfont (&bodyf, &keywordf, e_code);
  1160.           for (t_ptr = token; t_ptr < token_end; ++t_ptr)
  1161.         {
  1162.           check_code_size;
  1163.           *e_code++ = keywordf.allcaps && islower (*t_ptr)
  1164.             ? toupper (*t_ptr) : *t_ptr;
  1165.         }
  1166.           e_code = chfont (&keywordf, &bodyf, e_code);
  1167.         }
  1168.       else
  1169.         {
  1170.           /* Troff mode requires that strings be processed specially.  */
  1171.           if (troff && (*token == '"' || *token == '\''))
  1172.         {
  1173.           char qchar;
  1174.  
  1175.           qchar = *token;
  1176.           *e_code++ = '`';
  1177.           if (qchar == '"')
  1178.             *e_code++ = '`';
  1179.           e_code = chfont (&bodyf, &stringf, e_code);
  1180.  
  1181.           t_ptr = token + 1;
  1182.           while (t_ptr < token_end)
  1183.             {
  1184.               *e_code = *t_ptr++;
  1185.               if (*e_code == '\\')
  1186.             {
  1187.               *++e_code = '\\';
  1188.               if (*t_ptr == '\\')
  1189.                 *++e_code = '\\';
  1190.               /* Copy char after backslash.  */
  1191.               *++e_code = *t_ptr++;
  1192.               /* Point after the last char we copied.  */
  1193.               e_code++;
  1194.             }
  1195.             }
  1196.           e_code = chfont (&stringf, &bodyf, e_code - 1);
  1197.           if (qchar == '"')
  1198.             *e_code++ = '\'';
  1199.         }
  1200.           else
  1201.         for (t_ptr = token; t_ptr < token_end; ++t_ptr)
  1202.           {
  1203.             check_code_size;
  1204.             *e_code++ = *t_ptr;
  1205.           }
  1206.         }
  1207.       parser_state_tos->want_blank = true;
  1208.  
  1209.       /* If the token is va_dcl, it appears without a semicolon, so we
  1210.          need to pretend that one was there.  */
  1211.       if ((token_end - token) == 6
  1212.           && strncmp (token, "va_dcl", 6) == 0)
  1213.         {
  1214.           parser_state_tos->in_or_st = false;
  1215.           parser_state_tos->just_saw_decl--;
  1216.           parser_state_tos->in_decl = 0;
  1217.           parse (semicolon);
  1218.           force_nl = true;
  1219.         }
  1220.       break;
  1221.  
  1222.     case period:        /* treat a period kind of like a binary
  1223.                    operation */
  1224.       *e_code++ = '.';    /* move the period into line */
  1225.       parser_state_tos->want_blank = false;    /* dont put a blank after a
  1226.                            period */
  1227.       break;
  1228.  
  1229.     case comma:
  1230.       /* only put blank after comma if comma does not start the line */
  1231.       parser_state_tos->want_blank = (s_code != e_code);
  1232.       if (parser_state_tos->paren_depth == 0
  1233.           && parser_state_tos->in_decl
  1234.           && is_procname == 0
  1235.           && !parser_state_tos->block_init)
  1236.         while ((e_code - s_code) < (dec_ind - 1))
  1237.           {
  1238.         check_code_size;
  1239.         *e_code++ = ' ';
  1240.           }
  1241.  
  1242.       *e_code++ = ',';
  1243.       if (parser_state_tos->p_l_follow == 0)
  1244.         {
  1245.           if (parser_state_tos->block_init_level <= 0)
  1246.         parser_state_tos->block_init = 0;
  1247.           /* If we are in a declaration, and either the user wants all
  1248.              comma'd declarations broken, or the line is getting too
  1249.              long, break the line.  */
  1250.           if (break_comma &&
  1251.           (!leave_comma
  1252.            || (compute_code_target () + (e_code - s_code)
  1253.                > max_col - 8)
  1254.           ))
  1255.         force_nl = true;
  1256.         }
  1257.       break;
  1258.  
  1259.     case preesc:        /* got the character '#' */
  1260.       if ((s_com != e_com) ||
  1261.           (s_lab != e_lab) ||
  1262.           (s_code != e_code))
  1263.         dump_line ();
  1264.       *e_lab++ = '#';    /* move whole line to 'label' buffer */
  1265.       {
  1266.         int in_comment = 0;
  1267.         int com_start = 0;
  1268.         char quote = 0;
  1269.         int com_end = 0;
  1270.  
  1271.         /* ANSI allows spaces between '#' and preprocessor directives.
  1272.            Remove such spaces unless user has specified "-lpb".  */
  1273.         while (*buf_ptr == ' ' || *buf_ptr == '\t')
  1274.           {
  1275.         if (leave_preproc_space)
  1276.           *e_lab++ = *buf_ptr;
  1277.         buf_ptr++;
  1278.         if (buf_ptr >= buf_end)
  1279.           fill_buffer ();
  1280.           }
  1281.         while (*buf_ptr != '\n' || (in_comment && !had_eof))
  1282.           {
  1283.         check_lab_size;
  1284.         *e_lab = *buf_ptr++;
  1285.         if (buf_ptr >= buf_end)
  1286.           fill_buffer ();
  1287.         switch (*e_lab++)
  1288.           {
  1289.           case BACKSLASH:
  1290.             if (troff)
  1291.               *e_lab++ = BACKSLASH;
  1292.             if (!in_comment)
  1293.               {
  1294.             *e_lab++ = *buf_ptr++;
  1295.             if (buf_ptr >= buf_end)
  1296.               fill_buffer ();
  1297.               }
  1298.             break;
  1299.           case '/':
  1300.             if (*buf_ptr == '*' && !in_comment && !quote)
  1301.               {
  1302.             in_comment = 1;
  1303.             *e_lab++ = *buf_ptr++;
  1304.             com_start = e_lab - s_lab - 2;
  1305.               }
  1306.             break;
  1307.           case '"':
  1308.             if (quote == '"')
  1309.               quote = 0;
  1310.             break;
  1311.           case '\'':
  1312.             if (quote == '\'')
  1313.               quote = 0;
  1314.             break;
  1315.           case '*':
  1316.             if (*buf_ptr == '/' && in_comment)
  1317.               {
  1318.             in_comment = 0;
  1319.             *e_lab++ = *buf_ptr++;
  1320.             com_end = e_lab - s_lab;
  1321.               }
  1322.             break;
  1323.           }
  1324.           }
  1325.  
  1326.         while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
  1327.           e_lab--;
  1328.         if (e_lab - s_lab == com_end && bp_save == 0)
  1329.           {            /* comment on preprocessor line */
  1330.         if (save_com.end != save_com.ptr)
  1331.           {
  1332.             need_chars (save_com, 2);
  1333.             *save_com.end++ = '\n';    /* add newline between
  1334.                            comments */
  1335.             *save_com.end++ = ' ';
  1336.             --line_no;
  1337.           }
  1338.         need_chars (save_com, com_end - com_start);
  1339.         strncpy (save_com.end, s_lab + com_start,
  1340.              com_end - com_start);
  1341.         save_com.end += com_end - com_start;
  1342.  
  1343.         e_lab = s_lab + com_start;
  1344.         while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
  1345.           e_lab--;
  1346.         bp_save = buf_ptr;    /* save current input buffer */
  1347.         be_save = buf_end;
  1348.         buf_ptr = save_com.ptr;    /* fix so that subsequent calls to
  1349.                        lexi will take tokens out of
  1350.                        save_com */
  1351.         need_chars (save_com, 1);
  1352.         *save_com.end++ = ' ';    /* add trailing blank, just in case */
  1353.         buf_end = save_com.end;
  1354.         save_com.end = save_com.ptr;    /* make save_com empty */
  1355.           }
  1356.         *e_lab = '\0';    /* null terminate line */
  1357.         parser_state_tos->pcase = false;
  1358.       }
  1359.  
  1360.       if (strncmp (s_lab + 1, "if", 2) == 0)
  1361.         {
  1362.           if (blanklines_around_conditional_compilation)
  1363.         {
  1364.           register c;
  1365.           prefix_blankline_requested++;
  1366.           while ((c = *in_prog_pos++) == '\n');
  1367.           in_prog_pos--;
  1368.         }
  1369.           {
  1370.         /* Push a copy of the parser_state onto the stack. All
  1371.            manipulations will use the copy at the top of stack, and
  1372.            then we can return to the previous state by popping the
  1373.            stack.  */
  1374.         struct parser_state *new;
  1375.  
  1376.         new = (struct parser_state *)
  1377.           xmalloc (sizeof (struct parser_state));
  1378.         memcpy (new, parser_state_tos, sizeof (struct parser_state));
  1379.  
  1380.         /* We need to copy the dynamically allocated arrays in the
  1381.            struct parser_state too.  */
  1382.         new->p_stack = (enum codes *)
  1383.           xmalloc (parser_state_tos->p_stack_size
  1384.                * sizeof (enum codes));
  1385.         memcpy (new->p_stack, parser_state_tos->p_stack,
  1386.               parser_state_tos->p_stack_size * sizeof (enum codes));
  1387.  
  1388.         new->il = (int *)
  1389.           xmalloc (parser_state_tos->p_stack_size * sizeof (int));
  1390.         memcpy (new->il, parser_state_tos->il,
  1391.             parser_state_tos->p_stack_size * sizeof (int));
  1392.  
  1393.         new->cstk = (int *)
  1394.           xmalloc (parser_state_tos->p_stack_size
  1395.                * sizeof (int));
  1396.         memcpy (new->cstk, parser_state_tos->cstk,
  1397.             parser_state_tos->p_stack_size * sizeof (int));
  1398.  
  1399.         new->paren_indents = (short *) xmalloc
  1400.           (parser_state_tos->paren_indents_size * sizeof (short));
  1401.         memcpy (new->paren_indents, parser_state_tos->paren_indents,
  1402.              parser_state_tos->paren_indents_size * sizeof (short));
  1403.  
  1404.         new->next = parser_state_tos;
  1405.         parser_state_tos = new;
  1406.           }
  1407.         }
  1408.       else if (strncmp (s_lab + 1, "else", 4) == 0)
  1409.         {
  1410.           /* When we get #else, we want to restore the parser state to
  1411.              what it was before the matching #if, so that things get
  1412.              lined up with the code before the #if.  However, we do not
  1413.              want to pop the stack; we just want to copy the second to
  1414.              top elt of the stack because when we encounter the #endif,
  1415.              it will pop the stack.  */
  1416.           else_or_endif = true;
  1417.           if (parser_state_tos->next)
  1418.         {
  1419.           /* First save the addresses of the arrays for the top of
  1420.              stack.  */
  1421.           enum codes *tos_p_stack = parser_state_tos->p_stack;
  1422.           int *tos_il = parser_state_tos->il;
  1423.           int *tos_cstk = parser_state_tos->cstk;
  1424.           short *tos_paren_indents =
  1425.           parser_state_tos->paren_indents;
  1426.           struct parser_state *second =
  1427.           parser_state_tos->next;
  1428.  
  1429.           memcpy (parser_state_tos, second,
  1430.               sizeof (struct parser_state));
  1431.           parser_state_tos->next = second;
  1432.  
  1433.           /* Now copy the arrays from the second to top of stack to
  1434.              the top of stack.  */
  1435.           /* Since the p_stack, etc. arrays only grow, never shrink,
  1436.              we know that they will be big enough to fit the array
  1437.              from the second to top of stack.  */
  1438.           parser_state_tos->p_stack = tos_p_stack;
  1439.           memcpy (parser_state_tos->p_stack,
  1440.               parser_state_tos->next->p_stack,
  1441.               parser_state_tos->p_stack_size
  1442.               * sizeof (enum codes));
  1443.  
  1444.           parser_state_tos->il = tos_il;
  1445.           memcpy (parser_state_tos->il,
  1446.               parser_state_tos->next->il,
  1447.               parser_state_tos->p_stack_size * sizeof (int));
  1448.  
  1449.           parser_state_tos->cstk = tos_cstk;
  1450.           memcpy (parser_state_tos->cstk,
  1451.               parser_state_tos->next->cstk,
  1452.               parser_state_tos->p_stack_size * sizeof (int));
  1453.  
  1454.           parser_state_tos->paren_indents = tos_paren_indents;
  1455.           memcpy (parser_state_tos->paren_indents,
  1456.               parser_state_tos->next->paren_indents,
  1457.               parser_state_tos->paren_indents_size
  1458.               * sizeof (short));
  1459.         }
  1460.           else
  1461.         diag (1, "Unmatched #else");
  1462.         }
  1463.       else if (strncmp (s_lab + 1, "endif", 5) == 0)
  1464.         {
  1465.           else_or_endif = true;
  1466.           /* We want to remove the second to top elt on the stack, which
  1467.              was put there by #if and was used to restore the stack at
  1468.              the #else (if there was one). We want to leave the top of
  1469.              stack unmolested so that the state which we have been using
  1470.              is unchanged.  */
  1471.           if (parser_state_tos->next)
  1472.         {
  1473.           struct parser_state *second = parser_state_tos->next;
  1474.  
  1475.           parser_state_tos->next = second->next;
  1476.           free (second->p_stack);
  1477.           free (second->il);
  1478.           free (second->cstk);
  1479.           free (second->paren_indents);
  1480.           free (second);
  1481.         }
  1482.           else
  1483.         diag (1, "Unmatched #endif");
  1484.           if (blanklines_around_conditional_compilation)
  1485.         {
  1486.           postfix_blankline_requested++;
  1487.           n_real_blanklines = 0;
  1488.         }
  1489.         }
  1490.  
  1491.       /* Normally, subsequent processing of the newline character
  1492.          causes the line to be printed.  The following clause handles
  1493.          a special case (comma-separated declarations separated
  1494.          by the preprocessor lines) where this doesn't happen. */
  1495.       if (parser_state_tos->last_token == comma
  1496.           && parser_state_tos->p_l_follow <= 0
  1497.           && leave_comma && !parser_state_tos->block_init
  1498.           && break_comma && s_com == e_com)
  1499.         {
  1500.           dump_line ();
  1501.           parser_state_tos->want_blank = false;
  1502.         }
  1503.       break;
  1504.  
  1505.     case comment:        /* we have gotten a /*  this is a biggie */
  1506.       if (flushed_nl)
  1507.         {            /* we should force a broken line here */
  1508.           flushed_nl = false;
  1509.           dump_line ();
  1510.           parser_state_tos->want_blank = false;    /* dont insert blank at
  1511.                                line start */
  1512.           force_nl = false;
  1513.         }
  1514.       pr_comment ();
  1515.       break;
  1516.     }            /* end of big switch stmt */
  1517.  
  1518.       *e_code = '\0';        /* make sure code section is null terminated */
  1519.       if (type_code != comment
  1520.       && type_code != newline
  1521.       && type_code != preesc
  1522.       && type_code != form_feed)
  1523.     parser_state_tos->last_token = type_code;
  1524.  
  1525.     }                /* end of main while (1) loop */
  1526. }
  1527.  
  1528.  
  1529.  
  1530. char *set_profile ();
  1531. void set_defaults ();
  1532. int set_option ();
  1533.  
  1534. /* Points to current input file */
  1535. char *in_name = 0;
  1536.  
  1537. /* Points to the name of the output file */
  1538. char *out_name = 0;
  1539.  
  1540. /* How many input files were specified */
  1541. int input_files;
  1542.  
  1543. /* Names of all input files */
  1544. char **in_file_names;
  1545.  
  1546. /* Initial number of input filenames to allocate. */
  1547. int max_input_files = 128;
  1548.  
  1549.  
  1550. #ifdef DEBUG
  1551. int debug;
  1552. #endif
  1553.  
  1554. main (argc, argv)
  1555.      int argc;
  1556.      char **argv;
  1557. {
  1558.   register int i;
  1559.   struct file_buffer *current_input;
  1560.   char *profile_pathname = 0;
  1561.   int using_stdin = false;
  1562.  
  1563. #ifdef DEBUG
  1564.   if (debug)
  1565.     debug_init ();
  1566. #endif
  1567.  
  1568.   init_parser ();
  1569.   initialize_backups ();
  1570.  
  1571.   output = 0;
  1572.   input_files = 0;
  1573.   in_file_names = (char **) xmalloc (max_input_files * sizeof (char *));
  1574.  
  1575.   set_defaults ();
  1576.   for (i = 1; i < argc; ++i)
  1577.     if (strcmp (argv[i], "-npro") == 0
  1578.     || strcmp (argv[i], "--ignore-profile") == 0
  1579.     || strcmp (argv[i], "+ignore-profile") == 0)
  1580.       break;
  1581.   if (i >= argc)
  1582.     profile_pathname = set_profile ();
  1583.  
  1584.   for (i = 1; i < argc; ++i)
  1585.     {
  1586.       if (argv[i][0] != '-' && argv[i][0] != '+')    /* Filename */
  1587.     {
  1588.       if (expect_output_file == true)    /* Last arg was "-o" */
  1589.         {
  1590.           if (out_name != 0)
  1591.         {
  1592.           fprintf (stderr, "indent: only one output file (2nd was %s)\n", argv[i]);
  1593.           exit (1);
  1594.         }
  1595.  
  1596.           if (input_files > 1)
  1597.         {
  1598.           fprintf (stderr, "indent: only one input file when output file is specified\n");
  1599.           exit (1);
  1600.         }
  1601.  
  1602.           out_name = argv[i];
  1603.           expect_output_file = false;
  1604.           continue;
  1605.         }
  1606.       else
  1607.         {
  1608.           if (using_stdin)
  1609.         {
  1610.           fprintf (stderr, "indent: can't have filenames when specifying standard input\n");
  1611.           exit (1);
  1612.         }
  1613.  
  1614.           input_files++;
  1615.           if (input_files > 1)
  1616.         {
  1617.           if (out_name != 0)
  1618.             {
  1619.               fprintf (stderr, "indent: only one input file when output file is specified\n");
  1620.               exit (1);
  1621.             }
  1622.  
  1623.           if (use_stdout != 0)
  1624.             {
  1625.               fprintf (stderr, "indent: only one input file when stdout is used\n");
  1626.               exit (1);
  1627.             }
  1628.  
  1629.           if (input_files > max_input_files)
  1630.             {
  1631.               max_input_files = 2 * max_input_files;
  1632.               in_file_names = (char **) xrealloc (in_file_names,
  1633.                        (max_input_files * sizeof (char *)));
  1634.             }
  1635.         }
  1636.  
  1637.           in_file_names[input_files - 1] = argv[i];
  1638.         }
  1639.     }
  1640.       else
  1641.     {
  1642.       /* '-' as filename means stdin. */
  1643.       if (argv[i][0] == '-' && argv[i][1] == '\0')
  1644.         {
  1645.           if (input_files > 0)
  1646.         {
  1647.           fprintf (stderr, "indent: can't have filenames when specifying standard input\n");
  1648.           exit (1);
  1649.         }
  1650.  
  1651.           using_stdin = true;
  1652.         }
  1653.       else
  1654.         i += set_option (argv[i], (i < argc ? argv[i + 1] : 0), 1);
  1655.     }
  1656.     }
  1657.  
  1658.   if (verbose && profile_pathname)
  1659.     fprintf (stderr, "Read profile %s\n", profile_pathname);
  1660.  
  1661.   if (input_files > 1)
  1662.     {
  1663.       /* When multiple input files are specified, make a backup copy
  1664.      and then output the indented code into the same filename. */
  1665.  
  1666.       for (i = 0; input_files; i++, input_files--)
  1667.     {
  1668.       current_input = read_file (in_file_names[i]);
  1669.       in_name = out_name = in_file_names[i];
  1670.       output = fopen (out_name, "w");
  1671.       if (output == 0)
  1672.         {
  1673.           fprintf (stderr, "indent: can't create %s\n", out_name);
  1674.           exit (1);
  1675.         }
  1676.  
  1677.       make_backup (current_input);
  1678.       reset_parser ();
  1679.       indent (current_input);
  1680.       if (fclose (output) != 0)
  1681.         sys_error (out_name);
  1682.     }
  1683.     }
  1684.   else
  1685.     {
  1686.       /* One input stream -- specified file, or stdin */
  1687.  
  1688.       if (input_files == 0 || using_stdin)
  1689.     {
  1690.       input_files = 1;
  1691.       in_file_names[0] = "Standard input";
  1692.       current_input = read_stdin ();
  1693.     }
  1694.       else
  1695.     /* 1 input file */
  1696.     {
  1697.       current_input = read_file (in_file_names[0]);
  1698.       if (!out_name && !use_stdout)
  1699.         {
  1700.           out_name = in_file_names[0];
  1701.           make_backup (current_input);
  1702.         }
  1703.     }
  1704.       in_name = in_file_names[0];
  1705.  
  1706.       /* Uset stdout if it was specified ("-st"), or neither input
  1707.          nor output file was specified, or we're doing troff. */
  1708.       if (use_stdout || !out_name || troff)
  1709.     output = stdout;
  1710.       else
  1711.     {
  1712.       output = fopen (out_name, "w");
  1713.       if (output == 0)
  1714.         {
  1715.           fprintf (stderr, "indent: can't create %s\n", out_name);
  1716.           exit (1);
  1717.         }
  1718.     }
  1719.  
  1720.       reset_parser ();
  1721.       indent (current_input);
  1722.     }
  1723.  
  1724.   exit (0);
  1725. }
  1726.